package glemipsc.instructions;

import glemipsc.Address;
import glemipsc.Function;
import glemipsc.GLEMipsC;
import glemipsc.Type;
import glemipsc.implementations.Argument;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Or extends BaseInstruction implements Serializable, Instruction, Expression {
	private Expression op1, op2;
	private Type type;
	private Address addr;
	
	public Or(Expression op1, Expression op2) {
		this.op1 = op1;
		this.op2 = op2;
		type = Type.getCommonType(op1.getType(), op2.getType());
	}
	
	public Expression getOperand(int index) {
		if (index == 0)
			return op1;
		else if (index == 1)
			return op2;
		return null;
	}
	
	public int getNumberOfOperands() {
		return 2;
	}

	public int getSize() {
		return 0;
	}

	public Type getType() {
		return type;
	}

	public int getImmediateValue() {
		return (op1.getImmediateValue() != 0 || op2.getImmediateValue() != 0) ? 1 : 0;
	}

	public boolean isImmediate() {
		return op1.isImmediate() && op2.isImmediate();
	}

	public Address getStoreAddress() {
		return addr;
	}

	public int getStoredSize() {
		// TODO Auto-generated method stub
		return 0;
	}

	public boolean isStored() {
		return !isImmediate();
	}

	public void setStoreAddress(Address addr) {
		this.addr = addr;
	}

	public void computeInstruction(Argument result, List<glemipsc.implementations.Instruction> instructions, Function func) {
		glemipsc.implementations.Implementation impl = GLEMipsC.compiler.getEnvironment().implementation;
		op1.computeInstruction(result, instructions, func);
		ArrayList<glemipsc.implementations.Instruction> secondCondition = new ArrayList<glemipsc.implementations.Instruction>();
		op2.computeInstruction(result, secondCondition, func);
		instructions.add(impl.getJnz(result, impl.getImmediate(secondCondition.size())));
		instructions.addAll(secondCondition);
	}

}
